package com.valuegroup.smarttodo.data.service;

import com.valuegroup.smarttodo.data.model.AppStatus;
import com.valuegroup.smarttodo.data.model.Remark;
import com.valuegroup.smarttodo.data.model.RemarkDao;

import java.util.Date;
import java.util.List;

public class RemarkService extends BaseService {

    public Remark getDefaultRemark(Long relateId, Integer remarkType) {
        Remark r = new Remark();
        r.setRelateId(relateId);
        r.setRelateType(remarkType);
        r.setCreatedDt(new Date());
        r.setContent("");
        r.setUpdatedDt(new Date());
        r.setStatus(AppStatus.Status.NORMAL);
        return r;
    }

    public Remark saveRemark(int remarkType, long relateId, String text) {
        Remark r = getRemark(remarkType, relateId);
        if (r == null) {
            r = new Remark();
            r.setRelateId(relateId);
            r.setRelateType(remarkType);
            r.setCreatedDt(new Date());
            r.setStatus(AppStatus.Status.NORMAL);
        }
        r.setContent(text);
        r.setUpdatedDt(new Date());
        getRemarkDao().save(r);
        return r;
    }

    public Remark getRemark(int relateType, Long relateId) {
        List<Remark> list = getRemarkDao().queryBuilder()
                .where(RemarkDao.Properties.RelateId.eq(relateId))
                .where(RemarkDao.Properties.RelateType.eq(relateType))
                .list();
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    public void clearRemark(int relateType, Long relateId) {
        List<Remark> list = getRemarkDao().queryBuilder()
                .where(RemarkDao.Properties.RelateId.eq(relateId))
                .where(RemarkDao.Properties.RelateType.eq(relateType))
                .list();
        if (list != null && list.size() > 0) {
            getRemarkDao().deleteInTx(list);
        }
    }
}
